<?php
/*
	
*/
defined('BY_JYA') or exit('Access Denied');
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
$return=array();
$return['diy']=$this->plugin_setting('diy');
$return['setting']=$this->plugin_setting('basic');
if($_RQ['op']=='main'){
	$return['params']=array('status'=>10);
	$return['params']['type']=$_RQ['type']?:'guoshu_list';
	$return['list']=array();
	exi($return);
}
if($_RQ['op']=='getlist'){
	$params=array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']);
	if(isset($_RQ['status']) && $_RQ['status'] != 10){
			$params['status']=intval($_RQ['status']);
			$return['status']=$_RQ['status'];
	}else{
		$return['params']['status']=10;
	}
	$return['params']['type']=$_RQ['type']?:'guoshu_list';
	if($return['params']['type']=='land_list'){
		$params['land_id >']=0;
	}elseif($return['params']['type']=='animal_list'){
		$params['animal_id >']=0;
	}elseif($return['params']['type']=='seed_list'){
		$params['seed_id >']=0;
	}elseif($return['params']['type']=='guoshu_list'){
		$params['guoshu_id >']=0;
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$list=pdo_getslice('jy_farmer_order',$params,array($pindex,$psize),$total,$fields,'','createtime desc');
	if($list){
		foreach($list as &$item){
				$item['statustitle']=$this->order_status[$item['status']];
				$item['shoutypetitle']=$this->send_types[$item['sendtype']];
				if($return['params']['type']=='seed_list'){
					$seed=pdo_get('jy_farmer_seed',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['seed_id']));
					$item['image']=$seed['image'];
					$item['title']=$seed['title'];
				}elseif($return['params']['type']=='animal_list'){
					$animal=pdo_get('jy_farmer_animal',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['animal_id']));
					$item['image']=$animal['image'];
					$item['title']=$animal['title'];
					$item['atitle']=pdo_getcolumn('jy_farmer_aclassify',array('uniacid'=>$_SESSION['uniacid'],'id'=>$animal['classify_id']),'title');
				}elseif($return['params']['type']=='land_list'){
					$land=pdo_get('jy_farmer_land',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['land_id']));	
					$item['image']=$land['image'];
					$item['title']=$land['title'];
					$item['area']=$land['area'];
					$item['ltitle']=pdo_getcolumn('jy_farmer_lclassify',array('uniacid'=>$_SESSION['uniacid'],'id'=>$land['classify_id']),'title');
				}elseif($return['params']['type']=='guoshu_list'){
					$land=pdo_get('jy_farmer_guoshu',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['guoshu_id']),array('image','title','growtime','shouyue','shoukg','shoufee'));	
					$item['image']=$land['image'];
					$item['title']=$land['title'];
					$item['area']=$land['area'];
					$item['animal']['shoukg']=$land['shoukg']*$item['total'];
					$item['animal']['shoufee']=$land['shoufee']*$item['total'];
					$item['animal']['growtime']=$land['growtime'];
					$item['animal']['shouyue']=$land['shouyue'];
					$shouday=$item['createtime']+$land['growtime']*24*3600;
					$shouday=date('Y-m-d',$shouday);
					$item['animal']['shouday']=$shouday;
					$item['ltitle']=pdo_getcolumn('jy_farmer_gclassify',array('uniacid'=>$_SESSION['uniacid'],'id'=>$land['glassify_id']),'title');
				}
				$item['createtime']=date('Y-m-d H:i:s', $item['createtime']);
				
		}
	}
	$return['list']=$list;
	exi($return);
}
/*******op*******/
//订单详情
if($_RQ['op']=='detail'){
	if(!$_RQ['id']){
	exi('订单不存在，请重新选择','error');
	}
	$order=pdo_get('jy_farmer_order',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	//用户信息
	$member=pdo_get('core_members',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['mid']));
	//项目信息
	//$serve=pdo_get('jy_farmer_serve',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['serve_id']));	
	$order['serve_time']=$serve_times[$order['timeid']];
	$return['order']=$order;
	$return['user']=$user;
	$return['member']=$member;
	$return['serve']=$serve;
	exi($return);
}
//取消详情
if($_RQ['op']=='cancel'){
	if(!$_RQ['id']){
		exi('订单不存在，请重新选择','error');
	}
	pdo_update('jy_farmer_order',array('status'=>'3'),array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	$order=pdo_get('jy_farmer_order',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	//优惠券解锁
	if($order['couponid']){
		pdo_update('jy_weishop_coupon_getlog',array('islock'=>0),array('id'=>$order['couponid'],'uniacid'=>$_SESSION['uniacid']));
	}
	$this->model('notice')->notice_cancelorder($order);
	exi('取消成功');
}
if($_RQ['op']=='receive'){
	if(!$_RQ['id']){
		exi('订单不存在，请重新选择订单','error');
	}
	pdo_update('jy_farmer_order',array('status'=>'4'),array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	//确认订单后，计算员工佣金,存入佣金日志表，并更新员工表员工总佣金
	$worker_id=pdo_getcolumn('jy_farmer_order',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']),'worker_id');
	$pay=pdo_getcolumn('jy_farmer_order',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']),'pay');
	$lev_id=pdo_getcolumn('jy_farmer_worker',array('uniacid'=>$_SESSION['uniacid'],'id'=>$worker_id),'lev');
	$rate=pdo_getcolumn('jy_farmer_lev',array('uniacid'=>$_SESSION['uniacid'],'id'=>$lev_id),'rate');
	if($rate){
		$insert=array(
		'uniacid'=>$_SESSION['uniacid'],
		'worker_id'=>$worker_id,
		'order_id'=>$_RQ['id'],
		'money'=>$pay*$rate/100,
		'createtime'=>TIMESTAMP,
		);
		pdo_insert('jy_farmer_commission_log',$insert);
		$commission=pdo_getcolumn('jy_farmer_worker',array('uniacid'=>$_SESSION['uniacid'],'id'=>$worker_id),'commission');
		$commission=$commission+$pay*$rate/100;
		pdo_update('jy_farmer_worker',array('commission'=>$commission),array('uniacid'=>$_SESSION['uniacid'],'id'=>$worker_id));
	}
	
	exi('确认服务成功');
}
if($_RQ['op']=='del'){
	if(!$_RQ['id']){
		exi('订单不存在，请重新选择订单','error');
	}
	$order=pdo_get('jy_farmer_order',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	//优惠券解锁
	if($order['couponid']){
		pdo_update('jy_weishop_coupon_getlog',array('islock'=>0),array('id'=>$order['couponid'],'uniacid'=>$_SESSION['uniacid']));
	}
	pdo_delete('jy_farmer_order',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	exi('删除订单成功');
}
if($_RQ['op']=='pay'){
	if(!$_RQ['id']){
		exi('订单不存在，请重新选择订单','error');
	}
	$pay_id=pdo_getcolumn('jy_farmer_order',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']),'pay_id');
	exi($pay_id);
}